//
//  ZCClassificationVC.m
//  zctx
//
//  Created by apple on 15/11/27.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCClassificationVC.h"
#import "ZCClassificationCell.h"
@interface ZCClassificationVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)NSArray *dataArray;
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation ZCClassificationVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCClassificationVC"];
    [MTA trackPageViewBegin:@"ZCClassificationVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCClassificationVC"];
    [MTA trackPageViewEnd:@"ZCClassificationVC"];
}

-(void)updateRequest{
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus >0) {
        self.bannerView.hidden = YES;
    }
    [self initData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //AF 开启监听网络状态
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    self.navigationItem.title = @"食品分类";
    [self setNavigationItems];
    [self initData];
    [self initTableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCClassificationCell" bundle:nil] forCellReuseIdentifier:@"classCell"];
    
}

-(void)setNavigationItems {
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

- (void)backButtonPressed{
    ProgressDismiss;
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)initData{
//    NSString *paththree = [[NSBundle mainBundle]pathForResource:@"ZCclassification" ofType:@"plist"
//                           ];
//    self.dataArray = [[NSMutableArray alloc]initWithContentsOfFile:paththree];
    
    [NetworkManager requestFoundinggetsortsWithsuccess:^(NSArray *dictResponse) {
        self.dataArray = [dictResponse mutableCopy];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == 0) {
            self.bannerView.hidden = NO;
        }
    }];

}

-(void)initTableView{
    self.tableView  = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.dataSource =self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    self.tableView.separatorColor = RGB(236, 237, 238);
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:self.tableView];
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
       make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0,0));
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"classCell";
    ZCClassificationCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell ==nil) {
        cell = [[ZCClassificationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSDictionary *dict = self.dataArray[indexPath.row];
    NSString *titleStr = [NSString stringWithFormat:@"%@",[dict objectForKey:@"name"]];
    
     NSString *urlstr = [NSString stringWithFormat:@"%@%@",[NetworkManager strURLBasic],[dict objectForKey:@"thumb"]];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:urlstr]];
    cell.titleLable.text = titleStr;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 66;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dict = self.dataArray[indexPath.row];
    NSString *titleStr = [NSString stringWithFormat:@"%@",[dict objectForKey:@"name"]];
    NSString *idStr = [NSString stringWithFormat:@"%@",[dict objectForKey:@"id"]];
    self.cellselectClassification(titleStr,idStr);
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:(@selector(setLayoutMargins:))]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
